﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Goto"/>.
    /// </summary>
    public class GotoVM : StatementVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Goto),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new GotoVM((Goto)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected SymbolicRefVM _targetVM;
        protected ExpressionVM _constantExpressionVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Goto"/>.
        /// </summary>
        public GotoVM(Goto @goto, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(@goto, dictionary)
        {
            _targetVM = (SymbolicRefVM)CreateVM(@goto.Target, false, dictionary);
            _constantExpressionVM = (ExpressionVM)CreateVM(@goto.ConstantExpression, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Goto"/> model.
        /// </summary>
        public Goto Goto
        {
            get { return (Goto)CodeObject; }
        }

        /// <summary>
        /// The target <see cref="GotoTargetRef"/> (<see cref="LabelRef"/> or <see cref="SwitchItemRef"/>) or <see cref="UnresolvedRef"/>.
        /// </summary>
        public SymbolicRefVM Target
        {
            get { return _targetVM; }
        }

        /// <summary>
        /// The constant expression if this is a "goto case ...", otherwise null.
        /// </summary>
        public ExpressionVM ConstantExpression
        {
            get { return _constantExpressionVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return DarkPink; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.Pink; }
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            // If we have a constant expression ("goto case ..."), always render it instead of
            // the target reference.
            if (_constantExpressionVM != null)
            {
                renderer.RenderText(Case.ParseToken + " ", _targetVM.GetMessageBrush(KEYWORD_BRUSH, flags), this);
                _constantExpressionVM.Render(renderer, flags);
            }
            else
                _targetVM.Render(renderer, flags);
        }

        public override void UnRender()
        {
            if (_constantExpressionVM != null)
                _constantExpressionVM.UnRender();
            else
                _targetVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);

            // If we have a "goto case ...", show the hidden target reference in addition to the constant expression
            if (_constantExpressionVM != null && _targetVM != null)
            {
                renderer.NewLine();
                CreateVM(Goto.Target).RenderToolTip(renderer, RenderFlags.SuppressLineCol);
            }
        }

        #endregion
    }
}
